namespace XmlVisualizer
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    using System.Diagnostics;
    using System.Globalization;
    using System.IO;
    using System.Text;
    using System.Threading;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Windows.Threading;
    using System.Xml;
    using System.Xml.XPath;
    using Tomers.WPF.Localization;
    using XmlVisualizer.Exceptions;
    using XmlVisualizer.Models;

    /// <summary>
    /// Application class.
    /// </summary>
    public class App : System.Windows.Application
    {
        #region Methods
        /// <summary>
        /// Code that executes on application's startup.
        /// </summary>
        /// <param name="e">The event data.</param>
        protected override void OnStartup(System.Windows.StartupEventArgs e)
        {
            base.OnStartup(e);
            Mode mode = Mode.Xml;
            string filename = string.Empty;

            // process command line arguments
            foreach (string arg in e.Args)
            {
                if (arg.StartsWith("-mode="))
                {
                    string[] parts = arg.Split('=');
                    string modeString = parts[parts.Length - 1];
                    object m = Enum.Parse(typeof(Mode), modeString);
                    if (m != null)
                    {
                        mode = (Mode)m;
                    }
                }
                else
                {
                    filename = arg;
                }
            }

            Document doc = null;
            try
            {
                if (!string.IsNullOrEmpty(filename))
                {
                    // doc = new Document(filename);
                    doc = new Document();
                    doc.Controller.SetMode(mode);
                    doc.FileName = filename;
                    doc.Show();
                    doc.Load();
                }
                else
                {
                    doc = new Document();
                    doc.Controller.SetMode(mode);
                }

                if (doc != null)
                {
                    doc.ShowDocument();
                }
                else
                {
                    throw new Exception("Document creation failed.");
                }
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandling.HandleException(ex);
            }
            finally
            {
                if (doc == null)
                {
                    this.Shutdown();
                }
            }
        }
        #endregion
    }
}
